package com.contory.ssn_lgnd.app.maingame;

import android.util.Log;

import com.contory.ssn_lgnd.app.comp.UpdatePtr;
import com.contory.ssn_lgnd.framework.gl.Camera2D;
import com.contory.ssn_lgnd.framework.math.OverlapTester;
import com.contory.ssn_lgnd.framework.math.Rectangle;
import com.contory.ssn_lgnd.framework.math.Vector2;

public class PlayerTraceCam implements UpdatePtr{
	final Camera2D cam;
	final Player player;
	float lxBound;
	float rxBound;
	float uyBound;
	float dyBound;
	static final float boundRate = 0.6f;
	static final float boundHalfRate = 0.3f;
	
	public static final byte STATE_STOP = 0x00;
	public static final byte STATE_RIGHT_GO = 0x01;
	public static final byte STATE_LEFT_GO = 0x02;
	public static final byte STATE_UP_GO = 0x04;
	public static final byte STATE_DOWN_GO = 0x08;
	byte state = STATE_STOP;
	
	public PlayerTraceCam(Camera2D cam, Player player){
		this.cam = cam;
		this.player = player;
		lxBound = cam.getPosition().x - cam.getFrustumWidth() * boundHalfRate;
		rxBound = cam.getPosition().x + cam.getFrustumWidth() * boundHalfRate;
		uyBound = cam.getPosition().y - cam.getFrustumHeight() * boundHalfRate;
		dyBound = cam.getPosition().y + cam.getFrustumHeight() * boundHalfRate;
		state = STATE_STOP;
	}

	@Override
	public void update(float deltaTime) {
		camMovingUpdate();
		boundUpdate();
	}
	
	public void setState(byte state_){
		this.state = state_;
		//Log.d("1", String.valueOf(this.state));
	}
	
	private void camMovingUpdate(){
		//Log.d("2", String.valueOf(this.state));
		if(state == 0)
			return;
		if(player.getPosX() > rxBound){
			if((state & 1) == 1){
				cam.setPosX(player.getPosX() - cam.getFrustumWidth() * boundHalfRate);
			}
		}
		else if(player.getPosX() < lxBound){
			if(((state >> 1) & 1) == 1){
				cam.setPosX(player.getPosX() + cam.getFrustumWidth() * boundHalfRate);
			}
		}
		
		if(player.getPosY() > uyBound){
			if((state & 2) == 1){
				cam.setPosY(player.getPosY() - cam.getFrustumHeight() * boundHalfRate);
			}
		}
		else if(player.getPosY() < dyBound){
			if(((state >> 3) & 1) == 1){
				cam.setPosY(player.getPosY() + cam.getFrustumHeight() * boundHalfRate);
			}
		}
	}
	
	private void boundUpdate(){
		lxBound = cam.getPosition().x - cam.getFrustumWidth() * boundHalfRate;
		rxBound = cam.getPosition().x + cam.getFrustumWidth() * boundHalfRate;
		uyBound = cam.getPosition().y - cam.getFrustumHeight() * boundHalfRate;
		dyBound = cam.getPosition().y + cam.getFrustumHeight() * boundHalfRate;
	}
}
